package com.adayo.blesdkdemo;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.adayo.ble.bean.BLEDevice;

import java.util.List;

/**
 * Created by M02323 on 2017/9/15.
 */

public class BtAdapter extends BaseAdapter {
	protected Context context;
	protected LayoutInflater layoutInflater = null;
	protected List<BLEDevice> datas = null;

	public BtAdapter(Context context, List<BLEDevice> datas) {
		this.context = context;
		this.datas = datas;
		layoutInflater = LayoutInflater.from(this.context);
	}

	@Override
	public int getCount() {
		return datas.size();
	}

	@Override
	public Object getItem(int position) {
		return datas != null ? datas.get(position) : null;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int i, View view, ViewGroup viewGroup) {
		ViewHolder holder;
		if(view == null){
			holder = new ViewHolder();
			view = layoutInflater.inflate(R.layout.list_item, viewGroup, false);
			holder.tvAddress = (TextView) view.findViewById(R.id.tv_address);
			holder.tvName = (TextView) view.findViewById(R.id.tv_name);
			view.setTag(holder);
		}else{
			holder = (ViewHolder) view.getTag();
		}
		BLEDevice device = datas.get(i);
		holder.tvAddress.setText(device.getMac());
		holder.tvName.setText(device.getDeviceName());
		return view;
	}

	class ViewHolder {
		TextView tvAddress;
		TextView tvName;
	}
}
